package com.clhbolg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clhbolg.domain.JobLog;
import com.clhbolg.domain.bo.JobLogBo;
import com.clhbolg.domain.vo.JobLogVo;
import com.clhbolg.mapper.JobLogMapper;
import com.clhbolg.service.IJobLogService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 定时任务调度日志Service业务层处理
 *
 * @author cailinhu
 * @date 2023-02-08
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class JobLogServiceImpl implements IJobLogService {

    private final JobLogMapper baseMapper;

    /**
     * 查询定时任务调度日志
     */
    @Override
    public JobLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询定时任务调度日志列表
     */
    @Override
    public TableDataInfo<JobLogVo> queryPageList(JobLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JobLog> lqw = buildQueryWrapper(bo);
        Page<JobLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询定时任务调度日志列表
     */
    @Override
    public List<JobLogVo> queryList(JobLogBo bo) {
        LambdaQueryWrapper<JobLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<JobLog> buildQueryWrapper(JobLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<JobLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getJobId() != null, JobLog::getJobId, bo.getJobId());
        lqw.like(StringUtils.isNotBlank(bo.getJobName()), JobLog::getJobName, bo.getJobName());
        lqw.eq(StringUtils.isNotBlank(bo.getJobGroup()), JobLog::getJobGroup, bo.getJobGroup());
        lqw.eq(StringUtils.isNotBlank(bo.getInvokeTarget()), JobLog::getInvokeTarget, bo.getInvokeTarget());
        lqw.eq(StringUtils.isNotBlank(bo.getJobMessage()), JobLog::getJobMessage, bo.getJobMessage());
        lqw.eq(bo.getStatus() != null, JobLog::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getExceptionInfo()), JobLog::getExceptionInfo, bo.getExceptionInfo());
        lqw.eq(bo.getStartTime() != null, JobLog::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, JobLog::getEndTime, bo.getEndTime());
        return lqw;
    }

    /**
     * 新增定时任务调度日志
     */
    @Override
    public Boolean insertByBo(JobLogBo bo) {
        JobLog add = BeanUtil.toBean(bo, JobLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改定时任务调度日志
     */
    @Override
    public Boolean updateByBo(JobLogBo bo) {
        JobLog update = BeanUtil.toBean(bo, JobLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JobLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除定时任务调度日志
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
